<?php
class PaymentController extends BaseController{
    protected $payment_date=14;
    function init(){
        
    }
    function indexAction(){
        $id = $this->_request->id;
        $this->view->ID = $id;
        $this->view->USERNAME=$this->auth->email;
        $this->view->MOTOR_CODE = str_pad($id, 6 , "0", STR_PAD_LEFT);
        if ($this->type){
            $this->view->PAYMENT_CODE = 'MBX-'.$this->view->MOTOR_CODE.'-Y';
            $this->view->VND_PAYMENT_PRICE = '300.000';
            $this->view->USD_PAYMENT_PRICE = '15.00';
        }else{
            $this->view->PAYMENT_CODE = 'MBX-'.$this->view->MOTOR_CODE.'-X';
            $this->view->VND_PAYMENT_PRICE = '100.000';
            $this->view->USD_PAYMENT_PRICE = '5.00';
        }
        
        $this->view->KEY = base64_encode($this->auth->email.'-'.$id);
        
        $request = $this->getRequest();
        
        if ($request->isPost()){
            $this->_helper->viewRenderer->setNoRender();
            $mPayment = new Payment();
            $data = array (
                'motor_id'=>$request->id,
                'user_id'=>$this->auth->id,
                'firstname'=>$this->auth->first_name,
                'lastname'=>$this->auth->last_name,
                'price'=>$this->view->PAYMENT_PRICE,
                'orderdate'=>date ('Y-m-d H:i'),
                'months'=>$this->payment_date,
                'payment_code'=>$request->pcode,
                'status'=>0,
            );
            
            $id = $mPayment->insert($data);
            $mail = new Mail();
            $this->view->NAME = $this->auth->last_name .' '.$this->auth->first_name;
            $title = 'Hướng dẫn thanh toán trên website http://www.mua-ban-xe.com';
            $message = $this->view->render('payment/mail.phtml');
            $mail->sendEmail($this->auth->email, EMAIL_INFO, $title, $message);
            
            $this->_redirect('/');
            exit;
            //echo $this->view->render('payment/payment_success.phtml');
        }elseif($request->getParam('width')==''){
            $this->_redirect('/user/');
            exit;
        }else{            
            $this->_helper->layout->disableLayout();        
        }
        
    }//indexAction
    public function completeAction(){
        $this->_helper->viewRenderer->setNoRender();
        $key = $this->getRequest()->getParam('key', '');
        if ($key ==''){
            $this->_redirect ('/');    
        }
        $key = base64_decode($key);
        list ($email, $id) = explode('-', $key);
        
        $mMotor = new Motor();
        $arrData = array (
            'active'=>1,
            'premium'=>1,
            'expired_date'=>new Zend_Db_Expr('DATE_ADD(NOW(), INTERVAL '.$this->payment_date.'  DAY)')
        );
        $mMotor->updateMotor($arrData,$id);
        $this->load_header .= '<meta http-equiv="refresh" content="2;url='.DOMAIN.'" />';
        echo '<p>Thanh toán thành công. Tin của bạn đã được hiển thị trên website.</p>';
        
    }
    public function cancelAction(){
        $this->_helper->viewRenderer->setNoRender();
        $this->load_header .= '<meta http-equiv="refresh" content="2;url='.DOMAIN.'" />';
        echo '<p>Bạn đã hủy thanh toán. Tin của bạn chưa được hiển thị trên website.</p>';
    }
}    